<?php

namespace App\Winged\Patients\Controllers;

use App\Winged\Backend\Http\WingedHttp;
use App\Winged\Backend\Oss\OSS;
use App\Winged\Patients\Service\Helpers;
use Illuminate\Http\Request;
use App\Winged\Patients\Api\Api;

class UploadController extends BaseController
{


    public function upload(Request $request)
    {
        try {
            $path    = $request->input('path');
            $param   = $request->input('param');
            $file    = $request->file('file');
            $idParam = $request->input('idParam', null);
            $params  = [
                [
                    'name'     => $param,
                    'contents' => fopen($file->path(), 'r')
                ]
            ];
            if ($idParam != null) {
                $params[] = [
                    'name'     => $idParam,
                    'contents' => $request->input('idValue')
                ];
            }
            $params ['_request_content_type'] = WingedHttp::MULTIPART;
            list($status, $content) = Api::doUpload($path, $params);
            return response($content, $status);
        } catch (\Exception $e) {
            info($e->getMessage());
            return response()->json(['msg' =>  $e->getMessage(), 'status' => 500]);
        }
    }

    /**
     * oss上传接口 .
     *
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function ossUpload()
    {
        $file     = request()->file('file');
        $filename = Helpers::generateFileName($file);
        try {
            OSS::publicUploadFile($filename, $file->path());
            return response($filename);
        } catch (\Exception $e) {
            return response(['msg' => $e->getMessage()], 500);
        }
    }
}
